package com.intel.two.controller.medical;

import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;
import com.intel.two.pojo.medical.Armarium;
import com.intel.two.pojo.medical.Drugs;
import com.intel.two.service.medical.ArmariumService;
import com.intel.two.service.medical.DrugsService;
import com.intel.two.utils.OssUtil;
import com.intel.two.vo.medical.HealthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @Author:xiaofeia
 * @DATE:2022/10/14
 */
@RestController
@RequestMapping("/medical/drugs")
public class DrugsContorller {
    @Autowired
    private DrugsService drugsService;

//药品列表
    @RequestMapping("/list")
    public LayuiPageVo<Drugs> drugsList(@RequestParam Map<String, Object> params){
//        System.out.println(params);
        return drugsService.drugsPageData(params);
    }
    //添加药品信息
    @PostMapping("/add")
    public R addDrugs(@RequestBody Drugs drugs){
        System.out.println("drugs测试："+drugs);
        int result = drugsService.addDrugs(drugs);

        if(result==1){
            return R.ok();
        }else{
            return  R.error();
        }
    }
    //上传图片
    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws Exception {
        //得到文件的名字
        String originalFilename = file.getOriginalFilename();

        String fileUrl = OssUtil.updateLoad(file.getInputStream(), originalFilename);
        // 上传成功之后的文件地址 写给前端

        return R.ok().data("url",fileUrl);

    }

    @GetMapping("/delete")
    public R deleteDrugs(Integer drugs_id){
        //根据hid删除房屋信息
        System.out.println("drugs_id" + drugs_id);
        int result = drugsService.deleteDrugsById(drugs_id);
        if(result ==1){
            return R.ok().message("删除成功");
        }
        return R.error();
    }
    @PostMapping("/deleteIds")
    public R deletIdsDrugs(String drugs_ids){
        System.out.println(drugs_ids);
        int result = drugsService.deletDrugsIds(drugs_ids);
        if(result > 0){
            return R.ok().message("删除选中成功");
        }
        return R.error();

    }
    @PostMapping("/edit")
    public R updateDrugs(Drugs drugs){
        System.out.println(drugs);
        int result = drugsService.updateDrugs(drugs);
        if(result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }


}
